<?php
namespace app\admin\logic;
use app\admin\code\AuthCode;
use app\admin\code\BaseCode;
use app\admin\model\AuthModel;
use think\Lang;
use think\Request;

class AuthLogic extends BaseLogic
{
    public function auth($group='',$model='',$action=''){
        if(FOUNDER){
            // var_dump(FOUNDER);
            return true;
        }
        if(empty($group) || empty($model) || empty($action)){
            $request = Request::instance();
            $group = strtolower($request->module());
            $model = strtolower($request->controller());
            $action = strtolower($request->action());
        }
        //白名单 
        $white = array(
            'admin/admin/logout',
            'admin/login/index',
            'admin/admin/login',
            'admin/index/index',
            'admin/admin/edit_password',
        );
        if(in_array(strtolower($group.'/'.$model.'/'.$action), $white)){
            return true;
        }
        $info = model('authModel')->where(array('module'=>$group,'controller'=>$model,'action'=>$action))->find();
        if(!$info){
            return false;
        }
        if(!in_array($info['id'], session('admin.rule'))){
            return false;
        }
        return true;
    }
    protected function _after_del_validate(){
        $model = new AuthModel();
        if($model->deleteTrue(['id'=>$this->_param['id']])){
            $this->result = true;
            return true;
        }else{
            $this->err = ['code'=>BaseCode::FAIL,'msg'=>Lang::get(BaseCode::FAIL)];
            $this->result = false;
            return false;
        }
    }
    protected function _after_update_validate(){
        $model = new AuthModel();
        $where = [];
        $where['id'] = $this->_param['id'];
        unset($this->_param['id']);
        if($model->updateData($where,$this->_param)){
            $this->result = true;
            return true;
        }else{
            $this->err = ['code'=>BaseCode::FAIL,'msg'=>Lang::get(BaseCode::FAIL)];
            $this->result = false;
            return false;
        }
    }
    protected function _after_add_validate()
    {
        $model = new AuthModel();
        return $model->addData($this->_param);
    }
}


